/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.nju.software.greencreditsystem.service;

import cn.edu.nju.software.greencreditsystem.DAO.EnvironmentFactorDAO;
import cn.edu.nju.software.greencreditsystem.DAO.EnvironmentIndexDAO;
import cn.edu.nju.software.greencreditsystem.DAO.EnvironmentScoreDAO;
import cn.edu.nju.software.greencreditsystem.DTO.OutputEnvironmentFactorDTO;
import cn.edu.nju.software.greencreditsystem.domain.EnvironmentFactor;
import cn.edu.nju.software.greencreditsystem.domain.EnvironmentIndex;
import cn.edu.nju.software.greencreditsystem.domain.EnvironmentScore;
import cn.edu.nju.software.greencreditsystem.factory.DAOFactory;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：韩天
 * 创建时间：2011-8-21
 * 类描述：环境模块的service封装类
 */
public class EnvironmentService {
    
    /**
     * 方法描述：取得所有的环境评分指标
     * param：
     * return：SUCCESS
     */
    public List<EnvironmentFactor> get_AllScoreFactor(){
        EnvironmentFactorDAO efDAO = DAOFactory.getEnvironmentFactorDAOInstance();
        return efDAO.getAllScoreFactor();
    }
    
    /**
     * 方法描述：取得所有的环境核心指标
     * param：
     * return：SUCCESS
     */
    public List<EnvironmentFactor> get_AllIndexFactor(){
        EnvironmentFactorDAO efDAO = DAOFactory.getEnvironmentFactorDAOInstance();
        return efDAO.getAllIndexFactor();
    }
    
    /**
     * 方法描述：取得该企业的环境核心指标
     * param：
     * return：SUCCESS
     */
    public List<EnvironmentFactor> get_IndexFactor(int enterpriseId){
        EnvironmentFactorDAO efDAO = DAOFactory.getEnvironmentFactorDAOInstance();
        return efDAO.getIndexFactor(enterpriseId);
    }
    
    /**
     * 方法描述：取得所有的环境评分指标
     * param：
     * return：SUCCESS
     */
    public List<OutputEnvironmentFactorDTO> lookup_AllEnvironmentFactor(int enterpriseId){
        List<OutputEnvironmentFactorDTO> factorDTOLIst = new ArrayList<OutputEnvironmentFactorDTO>();
        //取得所有环境评分指标
        List<EnvironmentFactor> factorList = get_AllScoreFactor();
        EnvironmentScoreDAO esDAO = DAOFactory.getEnvironmentScoreDAOInstance();
        
        //取得每个环境评分指标的各项衡量数值
        for(int i=0; i<factorList.size(); i++){
            OutputEnvironmentFactorDTO factorDTO = new OutputEnvironmentFactorDTO();
            int factorId = factorList.get(i).getId();
            factorDTO.setFactorName(factorList.get(i).getName());
            factorDTO.setLatestValue(esDAO.getLatest(enterpriseId, factorId, 1).get(0).getValue());
            factorDTO.setUpdownValue(esDAO.getIncreaseNumber(enterpriseId, factorId));
            factorDTO.setUpdownMargin(esDAO.getIncreaseMargin(enterpriseId, factorId));
            factorDTO.setMaxValue(esDAO.getMax(enterpriseId, factorId));
            factorDTO.setMinValue(esDAO.getMin(enterpriseId, factorId));
            factorDTO.setTotalValue(factorList.get(i).getTotal());
            factorDTOLIst.add(factorDTO);
        }
        return factorDTOLIst;
    }
    
    /**
     * 方法描述：取得某个的环境评分指标的情况
     * param：
     * return：SUCCESS
     */
    public OutputEnvironmentFactorDTO lookup_A_ScoreFactor(int enterpriseId, int factorId){
        EnvironmentFactorDAO efDAO = DAOFactory.getEnvironmentFactorDAOInstance();
        EnvironmentScoreDAO esDAO = DAOFactory.getEnvironmentScoreDAOInstance();
        EnvironmentFactor factor = efDAO.getById(factorId); 
        //填满factorDTO的各项衡量数值
        OutputEnvironmentFactorDTO factorDTO = new OutputEnvironmentFactorDTO();
        factorDTO.setFactorName(factor.getName());
        factorDTO.setLatestValue(esDAO.getLatest(enterpriseId, factorId, 1).get(0).getValue());
        factorDTO.setUpdownValue(esDAO.getIncreaseNumber(enterpriseId, factorId));
        factorDTO.setUpdownMargin(esDAO.getIncreaseMargin(enterpriseId, factorId));
        factorDTO.setMaxValue(esDAO.getMax(enterpriseId, factorId));
        factorDTO.setMinValue(esDAO.getMin(enterpriseId, factorId));
        factorDTO.setTotalValue(factor.getTotal());
        return factorDTO;
    }
    
    /**
     * 方法描述：取得某个的环境核心指标的情况
     * param：
     * return：SUCCESS
     */
    public OutputEnvironmentFactorDTO lookup_A_IndexFactor(int enterpriseId, int factorId){
        EnvironmentFactorDAO efDAO = DAOFactory.getEnvironmentFactorDAOInstance();
        EnvironmentIndexDAO eiDAO = DAOFactory.getEnvironmentIndexDAOInstance();
        EnvironmentFactor factor = efDAO.getById(factorId); 
        //填满factorDTO的各项衡量数值
        OutputEnvironmentFactorDTO factorDTO = new OutputEnvironmentFactorDTO();
        factorDTO.setFactorName(factor.getName());
        factorDTO.setLatestValue(eiDAO.getLatest(enterpriseId, factorId, 1).get(0).getValue());
        factorDTO.setUpdownValue(eiDAO.getIncreaseNumber(enterpriseId, factorId));
        factorDTO.setUpdownMargin(eiDAO.getIncreaseMargin(enterpriseId, factorId));
        factorDTO.setMaxValue(eiDAO.getMax(enterpriseId, factorId));
        factorDTO.setMinValue(eiDAO.getMin(enterpriseId, factorId));
        
        return factorDTO;
    }
    
    /**
     * 方法描述：取得某个的环境评分指标的历史记录
     * param：
     * return：SUCCESS
     */
    public List<EnvironmentScore> get_ScoreFactorHistory(int enterpriseId, int factorId){
        EnvironmentScoreDAO esDAO = DAOFactory.getEnvironmentScoreDAOInstance();
        return esDAO.getLatest(enterpriseId, factorId, 0);
    }
    
    /**
     * 方法描述：取得某个的环境核心指标的历史记录
     * param：
     * return：SUCCESS
     */
    public List<EnvironmentIndex> get_IndexFactorHistory(int enterpriseId, int factorId){
        EnvironmentIndexDAO eiDAO = DAOFactory.getEnvironmentIndexDAOInstance();
        return eiDAO.getLatest(enterpriseId, factorId, 0);
    }
    
    /**
     * 方法描述：取得某个环境指标的折线图
     * param：
     * return：
     */
    public BufferedImage get_FactorImage(int enterpriseId, int factorId){
        EnvironmentFactorDAO efDAO = DAOFactory.getEnvironmentFactorDAOInstance();
        return efDAO.drawImage(enterpriseId, factorId);
    }
    
    /**
     * 方法描述：取得某个企业的环境总评
     * param：
     * return：
     */
    public double get_Total(int enterpriseId){
        EnvironmentScoreDAO ecDAO = DAOFactory.getEnvironmentScoreDAOInstance();
        return ecDAO.getLatestTotalScore(enterpriseId);
    }
}
